<!-- 销售管理 新建寄售采购单 响应-->
<template>
  <div class="sale-list">
    <el-container class="container-box">
      <!-- 列表 -->
      <RecordInfo
        :record-form ='responseDetail'
        :record-data ='headerInfo'
      >
        <template v-slot:recordInfoslot="slotProp">
          <div
            class="solt-info"
          >
            <span class="left-span">
              销售产品总数<span class="right-span">{{ headerInfo.prodTotalCount }}</span>
            </span>
            <span>
              品种数<span>{{ headerInfo.prodTypeCount }}</span>
            </span>
            <span>
              金额合计 （元）:
              <span ><span class='price'>{{ headerInfo.orderAmount | getFormatterPrice }}</span></span>
            </span>
          </div>
        </template>
      </RecordInfo>
      <el-editable
        class="hospital-edit-box"
        :data="pssCompareHspList"
        :row-style="getRowStyle"
        v-loading="tableLoading"
        :span-method="pssSpanMethod"
        :columns="responseTableData"
        @selection-change="handleSelectionChange"
      >
        <template
          slot="default-registrationNumber"
          slot-scope="scope">
          <div>
            <cell-registration
              :key="scope.row.registrationNumber"
              :regis-no="scope.row.registrationNumber"
              :content="'注册证已过期，点击查看详细信息'"
              :status="scope.row.isNoExpire"
              :regis-data="scope.row.registrationInfo || null"
            />
          </div>
          <!-- <el-button
            :disabled="!scope.row.registrationNumber||!scope.row.registrationId"
            v-html="regisNoButton(scope.row)"
            @click="showPdfRegistion(scope.row.registrationId)"
            type="text"
          /> -->
        </template>
      </el-editable>
      <el-footer>
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="curPage"
          :page-sizes="PAGE_NUMBER"
          :page-size="pageSize"
          :layout="PAGINATION_LAYOUT"
          :total="totalElements"
        />
      </el-footer>
    </el-container>
  </div>
</template>
<script>
import { responseTableData, responseDetail, ORDER_STATUS_TEXT } from './const'
import tabs from '@components/tabs/index.vue'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import RecordInfo from '@/moduleComponents/beforeSettle/distributorSide/purchasingManagement/record'
import { attrItemList } from '@const/orderCenterApi'
import {
  getHospitalPurchaseOrderItem,
  getHospitalPurchaseOrderList,
  getByBsAddress,
  distributorWscSaleOrderOrder,
  distriButorwscSaleOrderOrderOperate
} from '@const/beforeSettleApi'
import { getFormatterPrice } from '@const/filter'
import ElEditable from '@components/el-editable'
import CellRegistration from '@/moduleComponents/common/detail/cell-registration'
import { cloneDeep, isObject, hasIn } from 'lodash'
import { regisNoButton, showPdfRegistion } from '@const/agencySaas'
export default {
  components: { ElEditable, tabs, RecordInfo, CellRegistration },
  data () {
    return {
      // 生成采购订单路由地址
      makePurchasePath: '/distributorSide/newSalesManagement/makePurcharse',
      makePurchaseData: {
        wholesale: {
          type: 'wholesale'
        },
        consignment: {
          type: 'consignment'
        }
      },
      // 表单Loading
      formLoading: false,
      // 表格Loading
      tableLoading: false,
      // 表头
      responseTableData,
      // 表格数据
      pssContainHspList: [],
      pssCompareHspList: [],
      // 表单编辑默认值
      formdata: {},
      responseDetail,
      // 详情ID
      detailsId: this.$route.query.id,
      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      insideTableLoading: false,
      deliveryDetailList: [],
      activeSelectOrderList: [],
      activeIndex: 0,
      dialogPopupVisible: false,
      headerInfo: {}
    }
  },
  filters: {
    getFormatterPrice
  },
  computed: {
    ids () {
      return this.activeSelectOrderList.filter(i => i.saleDetailId).map(item => item.saleDetailId)
    }
  },
  mounted () {
    let arr = [
      { type: 'button', val: '返回', handle: this.back },
      { type: 'button', val: '拒绝', handle: this.goRefuse },
      { type: 'primary', val: '生成批发采购计划', handle: this.toMakeWholesalePurchase },
      { type: 'primary', val: '生成寄售采购计划', handle: this.toMakeConsignmentPurchase },
      { type: 'primary', val: '响应', handle: this.goResponse }
      // {type: 'primary', val: '推送', handle: this.back}
    ]
    const btn = {
      type: 'btn',
      arr: arr
    }
    this.$store.commit('changeLayoutHandle', btn)
    this.list()
  },

  methods: {
    regisNoButton,
    showPdfRegistion,
    // 选择订单
    handleSelectionChange (val) {
      this.activeSelectOrderList = val
    },
    // 生成采购补货单公用方法
    toMakePurchase (requestType) {
      const type = 'consignment'
      const path = this.makePurchasePath
      let ids = this.ids || []
      if (ids.length <= 0) {
        this.$message.warning('请选择产品')
        return
      }
      ids = JSON.stringify(ids)
      const {
        saleOrderId,
        saleOrderCode,
        applyDept,
        receiveAddress,
        receiveAddressId,
        contacts,
        contactNumber
      } = this.headerInfo
      const baseInfo = JSON.stringify({
        saleOrderId,
        saleOrderCode,
        applyDept,
        receiveAddress,
        receiveAddressId,
        contacts,
        contactNumber
      })
      const query = {
        ids,
        type,
        baseInfo,
        requestType
      }
      this.$router.push({
        path,
        query
      })
    },
    // 生成批发采购计划
    toMakeWholesalePurchase () {
      this.toMakePurchase('wholesale')
    },
    // 生成寄售采购计划
    toMakeConsignmentPurchase () {
      this.toMakePurchase('consignment')
    },
    // tab切换
    tabsHandle (index) {
      this.activeIndex = index
    },
    // 点击返回
    back () {
      this.$router.go(-1)
    },

    goResponse () {
      this.$axios({
        method: 'post',
        url: distriButorwscSaleOrderOrderOperate,
        data: {
          saleOrderId: this.$route.query.id,
          orderType: 2,
          operate: 1
        }
      }).then(res => {
        if (res.data.code === '0') {
          this.$message({ type: 'success', message: '响应成功' })
          this.$router.go(-1)
        } else {
          this.$message({ type: 'warning', message: '响应失败' })
        }
      })
    },
    goRefuse () {
      this.$axios({
        method: 'post',
        url: distriButorwscSaleOrderOrderOperate,
        data: {
          saleOrderId: this.$route.query.id,
          orderType: 2,
          operate: 2
        }
      }).then(res => {
        if (res.data.code === '0') {
          this.$message({ type: 'success', message: '拒绝成功' })
          this.$router.go(-1)
        } else {
          this.$message({ type: 'warning', message: '拒绝失败' })
        }
      })
    },
    // 获取详情列表
    list () {
      // Loading加载开始
      this.tableLoading = true
      // 请求列表数据
      this.$axios
        .$post(distributorWscSaleOrderOrder, {
          saleOrderId: this.$route.query.id,
          page: this.curPage,
          pageSize: this.pageSize,
          orderType: 2
        })
        .then(res => {
          let obj = hasIn(res, 'payload') ? res.payload : {}
          for (let key in obj) {
            if (key !== 'saleProductList') {
              if (!obj[key]) {
                obj[key] = `--`
              }
              if (key === 'orderStatus') {
                obj[key] = ORDER_STATUS_TEXT[obj[key]]
              }
              this.$set(this.headerInfo, [key], obj[key])
            } else if (key === 'saleProductList') {
              this.pssContainHspList = obj[key].content
              // 获取配送商和医院对比的list集合
              this.pssCompareHspList = this.getPssCompareHspList(this.pssContainHspList)
              this.totalElements = obj[key].totalElements
            }
          }
          this.tableLoading = false
        })
        .finally(res => {
          this.tableLoading = false
        })
    },
    /**
     * @des 获取产品信息对比list
     */
    getPssCompareHspList (list) {
      let result = []
      list = list || []
      list.forEach(e => {
        let info = e.hspItemInfo || {}
        let hspData = {
          saleDetailCode: '医院产品信息',
          prodCustomCode: info.prodCustomCode, // prodCustomCode
          prodName: info.prodName,
          prodSpecs: info.prodSpecs,
          prodModel: info.prodModel,
          minUnit: info.minUnit,
          registrationNumber: info.registrationNumber,
          price: info.price,
          isNoExpire: null,
          registrationInfo: null,
          manufacturer: info.manufacturer, // 生产厂家
          saleCount: info.purchaseCount,
          detailAmount: info.detailAmount,
          remarks: info.remarks,
          prodBigCategory: info.prodBigCategory
        }
        result.push(e)
        result.push(hspData)
      })
      return result
    },
    // 合并单元格
    pssSpanMethod ({ row, column, rowIndex, columnIndex }) {
      if (rowIndex % 2) {
        if (column.property === 'saleDetailCode') {
          return [1, 2]
        }
        if (column.property === 'relationDetailCode') {
          return [0, 0]
        }
        if (columnIndex === 0 || columnIndex === 1) {
          return [0, 0]
        }
      } else {
        if (columnIndex === 0 || columnIndex === 1) {
          return [2, 1]
        }
      }
    },
    // 奇数行灰色
    getRowStyle ({ row, rowIndex }) {
      if (rowIndex % 2) {
        return 'color: #A7B1C7;'
      }
    },
    // 页码更改
    handleSizeChange (size) {
      this.curPage = 1
      this.pageSize = size
      this.list()
    },

    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.list()
    }
  }
}
</script>

<style lang="less">
.sale-list {
  .header-search {
    .custom-search-form .filter-box .el-form-item__label {
      margin-right: 10px;
      white-space: nowrap;
    }
  }

  .container-box {
    height: 100%;
    overflow: auto;

    .btn-box {
      margin: 12px;
    }
  }
  .pagination {
    padding-top: 10px;
    display: flex;
    justify-content: flex-end;
    padding-right: 0;
  }
}
</style>
